<?php

/*
 * Edit by xinli
 * Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/PHPClass.php to edit this template
 */

namespace api\mall\controller;

use cmf\controller\RestBaseController;

use app\user\model\UserModel;
use api\mall\model\IMSessionModel;

/**
 * @OA\Tag(
 *     name="mall-im-session",
 *     description="会话管理"
 * )
 */
class IMSessionController extends RestBaseController
{
    protected function initialize()
    {
        parent::initialize();
        if (empty($this->user)) {
            $this->error('login');
        }
    }

    /**
     * 增加或更新会话 世界聊天发0
     * @OA\Post(
     *     tags={"mall-im-session"},
     *     path="/mall/iMSession/add",
     *     @OA\RequestBody(
     *         @OA\JsonContent(
     *             type="object",
     *             @OA\Property(property="receive_user_id", type="integer", example=6)
     *         )
     *     ),
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function add()
    {
        $data = $this->request->post();
        if (!isset($data['receive_user_id'])) {
            $this->error('parameter error');
        }
        $sendUserId = $this->getUserId();
        $receiveUserId = (int) $data['receive_user_id'];

        $sessionModel = new IMSessionModel();
        $session = $sessionModel->where('send_user_id', $sendUserId)->where('receive_user_id', $receiveUserId)->find();
        if (empty($session)) {
            $session = [
                'send_user_id' => $sendUserId,
                'receive_user_id' => $receiveUserId,
                'session_status' => 0,
                'create_time' => time(),
                'update_time' => time(),
            ];
            $sessionModel->insert($session);
        } else {
            if ($session['session_status'] == 1) {
                $this->error('禁言会话无法发送消息');
            }
            $sessionUpdate = [];
            $sessionUpdate['update_time'] = time();            
            $sessionModel->where('id', $session['id'])->update($sessionUpdate);
        }
        
        $session = $sessionModel->where('send_user_id', $sendUserId)->select();
        $this->success('ok', $session);
    }

    /**
     * 获取会话列表
     * @OA\Get(
     *     tags={"mall-im-session"},
     *     path="/mall/iMSession/select",
     *     @OA\Parameter(name="page",in="query",description="页码",required=false,@OA\Schema(type="integer")),
     *     @OA\Parameter(name="size",in="query",description="每页数量",required=false,@OA\Schema(type="integer")),
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function select()
    {
        $userid = $this->getUserId();
        $page = $this->request->param('page', 1, 'intval');
        $size = $this->request->param('size', 20, 'intval');
        $sessionModel = new IMSessionModel();
        $session = $sessionModel->where('send_user_id', $userid)->page($page, $size)->select();
        $this->success('ok', $session);
    }
    
    /**
     * 根据用户id获取用户头像 用来申请添加好友的时候显示
     * @OA\Post(
     *     tags={"mall-im-session"},
     *     path="/mall/iMSession/otherUserAvatar",
     *     @OA\RequestBody(
     *         @OA\JsonContent(
     *             type="object",
     *             @OA\Property(property="user_id", type="integer", example=6)
     *         )
     *     ),
     *     @OA\Response(response=200, ref="#/components/responses/200")
     * )
     */
    public function otherUserAvatar()
    {
        $data = $this->request->post();
        if (!isset($data['user_id'])) {
            $this->error('parameter error');
        }
        $otherUserId = (int) $data['user_id'];

        $userModel = new UserModel();
        $user = $userModel->where('id', $otherUserId)->find();
        if (empty($user)) {
            $this->error('user not found');
        }

        $user['avatar'] = cmf_get_image_url($user['avatar']);
        $this->success('ok', $user['avatar']);
    }
}
